//
//  ZSTopDownView.m
//  CheFu365
//
//  Created by safiri on 2017/9/25.
//  Copyright © 2017年 safiri. All rights reserved.
//

#import "ZSTopDownView.h"

#define Start_Height -64
#define Height 64
#define Image_Center_X 25
#define Image_Center_Y 40
#define Label_Y 20

#define Start_Height_IphoneX -112
#define Height_IphoneX 112
#define Image_Center_X_IphoneX 25
#define Image_Center_Y_IphoneX 80
#define Label_Y_IphoneX 60

#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Left_Offset 45
#define Right_Offset 20
#define Font_Size 14.0f

#define Image_Width 20

#define TDHUD_iPhoneXXS CGSizeEqualToSize(CGSizeMake(1125,2436), [[UIScreen mainScreen]currentMode].size)
#define TDHUD_iPhoneXR CGSizeEqualToSize(CGSizeMake(828,1792), [[UIScreen mainScreen]currentMode].size)
#define TDHUD_iPhoneXSMax CGSizeEqualToSize(CGSizeMake(1242,2688), [[UIScreen mainScreen]currentMode].size)

@interface ZSTopDownView ()

@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *hintLabel;
@property (nonatomic, assign) BOOL isIphoneXStyle;//是否是刘海鹏
@end

@implementation ZSTopDownView

//MARK: - init config
- (instancetype)init {
    self = [super init];
    if (self) {
        if (self.isIphoneXStyle) {
            self.frame = CGRectMake(0, Start_Height_IphoneX, Screen_Width, Height_IphoneX);
        }else {
            self.frame = CGRectMake(0, Start_Height, Screen_Width, Height);
        }
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)configHUDViewType:(HUDViewType)type message:(NSString *)hintMessage {
    [self configHUDViewType:type image:nil message:hintMessage messageTextColor:nil backgroundColor:nil];
}

- (void)configHUDViewType:(HUDViewType)type image:(UIImage *)hintImage message:(NSString *)hintMessage messageTextColor:(nullable UIColor *)textColor backgroundColor:(nullable UIColor *)backgroundColor {
    if (hintMessage == nil || hintMessage.length == 0) {
        return;
    }
    
    self.hudViewType = type;
    if (hintImage) {
        self.imageView.image = hintImage;
    }else {
        hintImage = [self getDefaultBundleImage:@"topdownHUD_hintSuccess@2x"];
        if (type == HUDViewTypeSuccess) {
            hintImage = [self getDefaultBundleImage:@"topdownHUD_hintSuccess@2x"];
        }else if (type == HUDViewTypeError) {
            hintImage = [self getDefaultBundleImage:@"topdownHUD_hintError@2x"];
        }else if (type == HUDViewTypeMessage) {
            hintImage = [self getDefaultBundleImage:@"topdownHUD_hintMessage@2x"];
        }else if (type == HUDViewTypeNetStatus) {
            hintImage = [self getDefaultBundleImage:@"topdownHUD_hintNet@2x"];
        }else if (type == HUDViewTypeWarning) {
            hintImage = [self getDefaultBundleImage:@"topdownHUD_hintWarn@2x"];
        }
        self.imageView.image = hintImage;
    }
    
    self.hintLabel.text = hintMessage;
    if (textColor) {
        self.hintLabel.textColor = textColor;
    }else {
        self.hintLabel.textColor = [UIColor colorWithRed:64/255.0f green:64/255.0f blue:64/255.0f alpha:1];
    }
    
    if (backgroundColor) {
        self.backgroundColor = backgroundColor;
    }else {
        self.backgroundColor = [UIColor whiteColor];
    }
    
    self.alpha = 1;
}

- (UIImage *)getDefaultBundleImage:(NSString *)imageName {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[ZSTopDownView class]] pathForResource:@"ZSTopDownHUD" ofType:@"bundle"]];
    NSString *path = [bundle pathForResource:imageName ofType:@"png"];
    return [UIImage imageWithContentsOfFile:path];
}

#pragma mark - 显示及隐藏动画
- (void)show {
    [UIView animateWithDuration:0.918f
                          delay:0
         usingSpringWithDamping:1.0f
          initialSpringVelocity:1.0f
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.center = CGPointMake(self.center.x, 32);
                         [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
                     }
                     completion:^(BOOL finished) { }];
    
}
- (void)hide:(void (^)(BOOL finished))completion {
    //0.218f
    [UIView animateWithDuration:0.218f
                          delay:0
         usingSpringWithDamping:1.0f
          initialSpringVelocity:20.0f
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.center = CGPointMake(self.center.x, -32);
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                         if (completion) {
                             completion(finished);
                         }
                     }];
    
}

// MARK: - getters
///左侧的类型icon
- (UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc]init];
        _imageView.userInteractionEnabled = YES;
        _imageView.frame = CGRectMake(0, 0, Image_Width, Image_Width);
        if (self.isIphoneXStyle) {
            _imageView.center = CGPointMake(Image_Center_X_IphoneX, Image_Center_Y_IphoneX);
        }else {
            _imageView.center = CGPointMake(Image_Center_X, Image_Center_Y);
        }
        
        [self addSubview:_imageView];
    }
    return _imageView;
}

///右侧的提示信息label
- (UILabel *)hintLabel {
    if (_hintLabel == nil) {
        _hintLabel = [[UILabel alloc]init];
        _hintLabel.numberOfLines = 0;
        _hintLabel.userInteractionEnabled = YES;
        if (self.isIphoneXStyle) {
            _hintLabel.frame = CGRectMake(Left_Offset, Label_Y_IphoneX, Screen_Width - Left_Offset - Right_Offset, 40);
        }else {
            _hintLabel.frame = CGRectMake(Left_Offset, Label_Y, Screen_Width - Left_Offset - Right_Offset, 40);
        }
        
        _hintLabel.textAlignment = NSTextAlignmentLeft;
        _hintLabel.font = [UIFont boldSystemFontOfSize:Font_Size];
        [self addSubview:_hintLabel];
    }
    return _hintLabel;
}

- (BOOL)isIphoneXStyle {
    return (TDHUD_iPhoneXXS || TDHUD_iPhoneXR || TDHUD_iPhoneXSMax);
}

@end
